//
//  SLAttentionCargoUserViewController.m
//  StarLight
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLAttentionCargoUserViewController.h"
#import "SLAttentionHeadCell.h"
#import "SLAttentionRowCell.h"
#import "SLAddNewFriendViewModel.h"
#import "SLFriendViewModel.h"
#import "SLHisBoatsViewController.h"
#import "SLHisGoodsViewController.h"


@interface SLAttentionCargoUserViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, strong) SLAddNewFriendViewModel *viewModel;

@property (nonatomic, strong) SLFriendViewModel *friendVM;

@end

@implementation SLAttentionCargoUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.friendVM getBuddylist];
    [self addUI];
    [self addObservers];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = @"关注货主";
    [self.viewModel getOperbuddylist];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)addObservers{
    @weakify(self);
    [[RACObserve(self.viewModel, operbuddyList) filter:^BOOL(NSMutableArray  *value) {
        return value != nil;
    }] subscribeNext:^(id x) {
        @strongify(self)
        [self.tableView reloadData];
        [[NSNotificationCenter defaultCenter] postNotificationName:kFriendChange object:nil];
    }];
    [[RACObserve(self.friendVM, friendList) filter:^BOOL(NSMutableArray *value) {
        return value.count > 0;
    }] subscribeNext:^(id x) {
        @strongify(self)
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    }];
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kFriendChange object:nil] subscribeNext:^(id x) {
        @strongify(self)
        [self.friendVM getBuddylist];
    }];
}

//同步联系人
- (void)synAdressPerson{
    [self.viewModel synAddresser];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.friendVM.friendList.count + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SLAttentionHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headCell"];
        if (cell == nil) {
            cell = [[SLAttentionHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }else{
        SLAttentionRowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rowCell"];
        if (cell == nil) {
            cell = [[SLAttentionRowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rowCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(SLAttentionRowCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section > 0) {
        [cell configFriendListCellWithModel:self.friendVM.friendList[indexPath.section - 1]];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 46;
    }else{
        return 81;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    //其他代码
    if (indexPath.section == 0) {
        [self synAdressPerson];
    }
    else{
        SLFriendModel *model = self.friendVM.friendList[indexPath.row];
        if (model.type == 0) {
            SLHisBoatsViewController *boatsVC = [SLHisBoatsViewController new];
            [boatsVC getMoreBoatWithHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
            [self.navigationController pushViewController:boatsVC animated:YES];
        }
        else{
            SLHisGoodsViewController *goodsVC = [SLHisGoodsViewController new];
            [goodsVC changeHid:[NSString stringWithFormat:@"%ld",(long)model.fid]];
            [self.navigationController pushViewController:goodsVC animated:YES];
        }
    }
}
#pragma mark - addUI
- (void)addUI{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
#pragma mark - getter
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (SLAddNewFriendViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [SLAddNewFriendViewModel new];
    }
    return _viewModel;
}
- (SLFriendViewModel *)friendVM{
    if (!_friendVM) {
        _friendVM = [SLFriendViewModel new];
    }
    return _friendVM;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
